

STYLE_DEFAULT = "\e[0m"
STYLE_BRIGHT = "\e[1m"
STYLE_UNDERLINE = "\e[4m"
STYLE_FLASH = "\e[5m"
STYLE_INVERSE = "\e[7m"
STYLE_INVISABLE = "\e[8m"

COLOR_BLACK_F = "\e[30m"
COLOR_RED_F = "\e[31m"
COLOR_GREEN_F = "\e[32m"
COLOR_YELLOW_F = "\e[33m"
COLOR_BLUE_F = "\e[34m"
COLOR_PURPLE_F = "\e[35m"
COLOR_CYAN_F = "\e[36m"
COLOR_WHITE_F = "\e[37m"
COLOR_BLACK_B = "\e[40m"
COLOR_RED_B = "\e[41m"
COLOR_GREEN_B = "\e[42m"
COLOR_YELLOW_B = "\e[43m"
COLOR_BLUE_B = "\e[44m"
COLOR_PURPLE_B = "\e[45m"
COLOR_CYAN_B = "\e[46m"
COLOR_WHITE_B = "\e[47m"


include ProjectConfig.mk

CCPP := $(PREFIX)g++
CC := $(PREFIX)gcc
AS := $(PREFIX)gcc -x assembler-with-cpp
LD := $(PREFIX)g++
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size


ifneq ($(DEBUG),0)
CPP_FLAGS += -g -gdwarf-2
C_FLAGS += -g -gdwarf-2
ASM_FLAGS += -g -gdwarf-2
endif
C_CPP_ASM_FLAGS += $(OPTIMIZATION) -Wall -fdata-sections -ffunction-sections
CPP_FLAGS += $(FLAGS) $(C_CPP_ASM_FLAGS)
C_FLAGS += $(FLAGS) $(C_CPP_ASM_FLAGS)
ASM_FLAGS += $(FLAGS) $(C_CPP_ASM_FLAGS)
LD_FLAGS += $(FLAGS) -specs=nano.specs -Wl,--gc-sections $(addprefix -L,$(LIB_PATHS)) $(addprefix -l,$(LIBS)) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref


# 预编译宏, 头文件路径
CPP_DEF_INC_FLAGS := $(addprefix -D,$(CPP_DEFINES)) $(addprefix -I,$(C_CPP_INCLUDE_PATHS))
C_DEF_INC_FLAGS := $(addprefix -D,$(C_DEFINES)) $(addprefix -I,$(C_CPP_INCLUDE_PATHS))
ASM_DEF_INC_FLAGS := $(addprefix -D,$(ASM_DEFINES)) $(addprefix -I,$(ASM_INCLUDE_PATHS))


# 目标文件
CPP_OBJECTS := $(addprefix $(BUILD_DIR)/,$(CPP_SOURCES:.cpp=.o))
C_OBJECTS := $(addprefix $(BUILD_DIR)/,$(C_SOURCES:.c=.o))
ASM_OBJECTS := $(addprefix $(BUILD_DIR)/,$(patsubst %.s,%.o,$(patsubst %.asm,%.o,$(ASM_SOURCES))))
OBJECTS := $(CPP_OBJECTS) $(C_OBJECTS) $(ASM_OBJECTS)

# 所有的目标文件路径
BUILD_DIRS = $(sort $(dir $(OBJECTS)))




BIN = $(CP) -O binary -S
HEX = $(CP) -O ihex
MOT = $(CP) -O srec
ifneq ($(DEBUG),0)
GenarateASM = $(PREFIX)objdump -S -d
else
GenarateASM = echo DebugDisable!
endif




# 如果无文件夹 $(BUILD_DIRS) 则创建
$(BUILD_DIRS):
	@ mkdir -p $@

# CPP文件编译规则
$(BUILD_DIR)/%.o: %.cpp
	@echo -e (COLOR_CYAN_F)CCPP$(STYLE_DEFAULT) $<
	@ $(CCPP) -c $(CPP_FLAGS) -MMD -MP -MF"$(@:%.o=%.d)" -Wa,-a,-ad,-alms=$(@:.o=.lst) $(CPP_DEF_INC_FLAGS) $< -o $@

# C文件编译规则
$(BUILD_DIR)/%.o: %.c
	@echo -e $(COLOR_YELLOW_F)CC$(STYLE_DEFAULT) $<
	@ $(CC) -c $(C_FLAGS) -MMD -MP -MF"$(@:%.o=%.d)" -Wa,-a,-ad,-alms=$(@:.o=.lst) $(C_DEF_INC_FLAGS) $< -o $@

# 汇编文件编译规则
$(BUILD_DIR)/%.o: %.s
	@echo -e $(COLOR_PURPLE_F)AS$(STYLE_DEFAULT) $<
	@ $(AS) -c $(ASM_FLAGS) $< -o $@

#elf生成规则
$(BUILD_DIR)/$(TARGET).elf: $(BUILD_DIRS) $(OBJECTS)
	@echo -e $(COLOR_GREEN_F)LD $@ $(STYLE_DEFAULT)
	@ $(LD) $(OBJECTS) $(OBJ_SOURCES) $(LD_FLAGS) -o $@
	$(SZ) $@

#hex生成规则
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf
	@echo Genarate HEX
	@ $(HEX) $< $@

#mot生成规则
$(BUILD_DIR)/%.mot: $(BUILD_DIR)/%.elf
	@echo -e $(COLOR_GREEN_F)Genarate MOT $(STYLE_DEFAULT)
	@ $(MOT) $< $@

#反汇编文件生成规则
$(BUILD_DIR)/%.s: $(BUILD_DIR)/%.elf
	@echo -e $(COLOR_PURPLE_F)Genarate ASM $(STYLE_DEFAULT)
	@ $(GenarateASM) $< > $@




all: $(BUILD_DIR)/$(TARGET).elf \
$(BUILD_DIR)/$(TARGET).hex \
$(BUILD_DIR)/$(TARGET).s

.PHONY: clean
clean:
	@echo clean...
	@ rm -fr $(BUILD_DIR)

.PHONY: info
info:
	@echo -e $(COLOR_BLUE_F)compiler:$(STYLE_DEFAULT) $(PREFIX)g++ $(PREFIX)gcc"\r\n"\
$(COLOR_BLUE_F)CPP_DEF_INC_FLAGS$(STYLE_DEFAULT) $(CPP_DEF_INC_FLAGS)"\r\n"\
$(COLOR_BLUE_F)C_DEF_INC_FLAGS$(STYLE_DEFAULT) $(C_DEF_INC_FLAGS)"\r\n"\
$(COLOR_BLUE_F)CPP_FLAGS$(STYLE_DEFAULT) $(CPP_FLAGS)"\r\n"\
$(COLOR_BLUE_F)C_FLAGS$(STYLE_DEFAULT) $(C_FLAGS)"\r\n"\
$(COLOR_BLUE_F)ASM_FLAGS$(STYLE_DEFAULT) $(ASM_FLAGS)"\r\n"\
$(COLOR_BLUE_F)LD_FLAGS$(STYLE_DEFAULT) $(LD_FLAGS)




sinclude $(OBJECTS:.o=.d)



